﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


namespace 设计模式
{
    public class Player
    {
        public int PosX {get; private set;}
        public int PosY {get; private set;}
        public void MoveLeft()
        {
            PosX--;
        }
        public void MoveRight()
        {
            PosX++;
        }
        public void MoveUp()
        {
            PosY++;
        }
        public void MoveDown()
        {
            PosY--;
        }
 
        public void Attack()
        {
            Debug.Log("Attack");
        }
 
        private static Player _player = new Player ();
        public static Player Instance
        {
            get {
                return _player;
            }
        }
        private Player()
        {
        }
    }

    public class PlayerContext
    {
        private string _tex;

        public string Text
        {
            get { return _tex; }

            set { _tex = value; }
        }
    }

    public abstract class Expression
    {
        public static void Interpret(PlayerContext context)
        {
            if (context.Text.Length == 0) {
                return;
            }
            string expStr = context.Text.Split (';')[0];
            Debug.Log("expStr:\t"+expStr);
            string[] strs = expStr.Split (' ');
            Debug.Log(strs);
            string cmd = strs [0];
            Debug.Log(cmd);
            string prm = null;
            if (strs.Length > 1) {
                prm = strs [1];
            }
            Expression exp = null;
            switch (cmd) {
                case "Move":
                    exp = new MoveExpression ();
                    break;
                case "Hit":
                    exp = new HitExpression ();
                    break;
            }
            if (exp != null) {
                exp.Execute (prm);
            }
            context.Text = context.Text.Remove (0, expStr.Length + 1);
        }
        public abstract void Execute (string prm);
    }

    
    public class MoveExpression : Expression
    {
        public override void Execute(string prm)
        {
            switch (prm) {
                case "Left":
                    Player.Instance.MoveLeft ();
                    break;
                case "Right":
                    Player.Instance.MoveRight ();
                    break;
                case "Up":
                    Player.Instance.MoveUp ();
                    break;
                case "Down":
                    Player.Instance.MoveDown ();
                    break;
            }
        }
    }
 
    public class HitExpression : Expression
    {
        public override void Execute(string prm)
        {
            Player.Instance.Attack ();
        }
    }

    public class HitAndRunExpression : Expression
    {
        private HitExpression _hitExp = new HitExpression();
        private MoveExpression _moveExp = new MoveExpression();
        public override void Execute(string prm)
        {
            _hitExp.Execute (prm);
            _moveExp.Execute (prm);
        }
    }

    
    
    public class Interpreter : MonoBehaviour
    {
        private void Start()
        {
            
             PlayerContext context = new PlayerContext();
             context.Text = "Move Up;Move Right;Hit&Run Down;";
                while (context.Text.Length > 0) {
                    MoveExpression.Interpret (context);
                }
                Debug.Log(Player.Instance.PosX);
                Debug.Log(Player.Instance.PosY);
        }


        


    }
    

}


